package com.cw.jeeyt.dao.impl.security;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.cw.jeeyt.dao.domain.security.SecurityUserModel;
import com.cw.jeeyt.dao.intf.security.SecurityUserDao;
import com.cw.jeeyt.dao.mapper.security.SecurityUserMapper;
import com.cw.jeeyt.dao.model.security.SecurityUserDo;
import com.cw.jeeyt.dao.model.security.SecurityUserExample;
import com.cw.jeeyt.dao.query.security.SecurityUserQuery;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.common.utils.PageUtil;
import com.cw.lang.common.utils.StringUtil;
import com.cw.lang.common.dao.AbstractBaseDaoImpl;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.mybatis.dto.PageInfo;

/**
 * 用户信息 dao数据库操作接口
 *
 * @author chenw
 * @version 1.0.0
 */
@Repository
public class SecurityUserDaoImpl extends AbstractBaseDaoImpl<SecurityUserDo, SecurityUserMapper> implements
    SecurityUserDao {
    @Autowired
    private SecurityUserMapper securityUserMapper;

    /**
     * 删除用户信息
     *
     * @param user
     */
    @Override
    public void delete(SecurityUserModel user) {
        securityUserMapper.delete(user);
    }

    /**
     * 修改用户信息
     *
     * @param user
     */
    @Override
    public Integer update(SecurityUserModel user) {
        return securityUserMapper.update(user);
    }

    /**
     * 根据用户名称查询用户信息
     *
     * @param username
     * @return
     * @throws Exception
     */
    @Override
    public SecurityUserModel findUserByName(String username) {
        return securityUserMapper.findUserByName(username);
    }

    /**
     * 根据主键查询用户信息
     *
     * @param id 主键id
     * @return 用户信息
     */
    @Override
    public SecurityUserModel getById(Long id) {
        return securityUserMapper.getById(id);
    }

    /**
     * 根据主键查询用户信息
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public SecurityUserModel getUserVoById(Long id) {
        return securityUserMapper.getUserVoById(id);
    }

    /**
     * 新增用户信息
     *
     * @param user
     * @return 整数
     */
    @Override
    public Integer insert(SecurityUserModel user) {
        return securityUserMapper.insert(user);
    }

    /**
     * 查询所有用户信息
     *
     * @return 用户信息集合
     */
    @Override
    public List<SecurityUserModel> queryUserList() {
        return securityUserMapper.queryUserList();
    }

    /**
     * 分页查询用户信息
     *
     * @param user 查询条件
     * @param startNum 开始条数
     * @param pageSize 每页长度
     * @return 用户信息集合
     */
    @Override
    public PageInfo<SecurityUserModel> queryByPage(SecurityUserModel user, Integer startNum, Integer pageSize) {
        PageHelper.startPage(PageUtil.calPageNum(startNum, pageSize), pageSize);
        return new PageInfo<>(securityUserMapper.queryByPage(user));
    }

    /**
     * 根据机构ID获取用户列表信息
     *
     * @param orgId
     * @return
     */
    @Override
    public List<SecurityUserDo> getUserListByOrgId(Long orgId) {
        return securityUserMapper.getUserListByOrgId(orgId);
    }

    /**
     * 根据角色ID获取用户列表
     *
     * @param roleId
     * @return
     */
    @Override
    public List<SecurityUserModel> getUserListByRoleId(Long roleId) {
        return securityUserMapper.getUserListByRoleId(roleId);
    }

    /**
     * 更新用户状态
     *
     * @param account
     * @param userState
     */
    @Override
    public void updateUserState(String account, String userState) {
        securityUserMapper.updateUserState(account, userState);
    }

    /**
     * 更新用户机构ID
     *
     * @param user
     * @return
     */
    @Override
    public int updateUserDefaultOrgId(SecurityUserModel user) {
        return securityUserMapper.updateUserDefaultOrgId(user);
    }

    /**
     * 根据账号查询用户信息
     *
     * @param account
     * @return
     */
    @Override
    public SecurityUserDo getUserByAccount(String account) {
        return securityUserMapper.getUserByAccount(account);
    }

    /**
     * 根据机构ID和类型查询用户列表信息
     */
    @Override
    public List<SecurityUserDo> getUserListByDefaultOrgIdAndType(Long defaultOrgId, String type) {
        return securityUserMapper.getUserListByDefaultOrgIdAndType(defaultOrgId, type);
    }

    /**
     * 检查登录用户账号密码
     *
     * @param account
     * @param password
     * @return
     */
    @Override
    public SecurityUserModel checkUser(String account, String password) {
        return securityUserMapper.checkUser(account, password);
    }

    /**
     * 根据条件查询用户信息
     *
     * @param user
     * @return
     */
    @Override
    public List<SecurityUserModel> queryListByCondition(SecurityUserModel user) {
        return securityUserMapper.queryListByCondition(user);
    }

    /**
     * 根据userid查询用户信息
     *
     * @param userId
     * @return
     */
    @Override
    public SecurityUserDo getUserByUserId(String userId) {
        return securityUserMapper.getUserByUserId(userId);
    }

    /**
     * 修改用户密码
     *
     * @param securityUser
     */
    @Override
    public void updateUserPassword(SecurityUserModel securityUser) {
        securityUserMapper.updateUserPassword(securityUser);
    }

    /**
     * 查询用户信息
     *
     * @param securityUser 条件
     * @return List<SecurityUserDo>
     */
    @Override
    public List<SecurityUserDo> selectByExample(SecurityUserQuery securityUser){
        return getMapper().selectByExample(buildExample(securityUser));
    }
    /**
     * 新增用户信息
     *
     * @param securityUser 条件
     * @return int>
     */
    @Override
    public int insertBySelective(SecurityUserDo securityUser){
        if (securityUser == null) {
            return -1;
        }
        securityUser.setCreateDate(new Date());
        securityUser.setUpdateDate(new Date());
        return getMapper().insertSelective(securityUser);
    }
    /**
     * 修改用户信息
     *
     * @param securityUser
     * @return
     */
    @Override
    public int updateBySelective(SecurityUserDo securityUser) {
        if (securityUser == null) {
            return -1;
        }
        securityUser.setUpdateDate(new Date());
        return getMapper().updateByPrimaryKeySelective(securityUser);
    }
    /**
     * 构建用户信息Example信息
     *
     * @param securityUser
     * @return
     */
    public SecurityUserExample buildExample(SecurityUserQuery securityUser){
        SecurityUserExample example = new SecurityUserExample();
        SecurityUserExample.Criteria criteria = example.createCriteria();
        if (securityUser != null) {
            //添加查询条件
            if (null != securityUser.getId()){
                criteria.andIdEqualTo(securityUser.getId());
            }
            if(CollectionUtil.isNotEmpty(securityUser.getIds())){
                criteria.andIdIn(securityUser.getIds());
            }
            if (StringUtil.isNotEmpty(securityUser.getUsername())){
                criteria.andUsernameEqualTo(securityUser.getUsername());
            }
            if (StringUtil.isNotEmpty(securityUser.getPassword())){
                criteria.andPasswordEqualTo(securityUser.getPassword());
            }
            if (StringUtil.isNotEmpty(securityUser.getAccount())){
                criteria.andAccountEqualTo(securityUser.getAccount());
            }
            if (StringUtil.isNotEmpty(securityUser.getMobile())){
                criteria.andMobileEqualTo(securityUser.getMobile());
            }
            if (StringUtil.isNotEmpty(securityUser.getTelephone())){
                criteria.andTelephoneEqualTo(securityUser.getTelephone());
            }
            if (StringUtil.isNotEmpty(securityUser.getStatus())){
                criteria.andStatusEqualTo(securityUser.getStatus());
            }
            if (StringUtil.isNotEmpty(securityUser.getComment())){
                criteria.andCommentEqualTo(securityUser.getComment());
            }
            if (StringUtil.isNotEmpty(securityUser.getUserState())){
                criteria.andUserStateEqualTo(securityUser.getUserState());
            }
            if (StringUtil.isNotEmpty(securityUser.getSex())){
                criteria.andSexEqualTo(securityUser.getSex());
            }
            if (StringUtil.isNotEmpty(securityUser.getPosition())){
                criteria.andPositionEqualTo(securityUser.getPosition());
            }
            if (StringUtil.isNotEmpty(securityUser.getEmail())){
                criteria.andEmailEqualTo(securityUser.getEmail());
            }
            if (StringUtil.isNotEmpty(securityUser.getType())){
                criteria.andTypeEqualTo(securityUser.getType());
            }
            if (StringUtil.isNotEmpty(securityUser.getExtend())){
                criteria.andExtendEqualTo(securityUser.getExtend());
            }
            if (null != securityUser.getDefaultOrgId()){
                criteria.andDefaultOrgIdEqualTo(securityUser.getDefaultOrgId());
            }
            if (null != securityUser.getCreateBy()){
                criteria.andCreateByEqualTo(securityUser.getCreateBy());
            }
            if (null != securityUser.getCreateDateBegin()){
                criteria.andCreateDateGreaterThanOrEqualTo(securityUser.getCreateDateBegin());
            }
            if (null != securityUser.getCreateDateEnd()){
                criteria.andCreateDateLessThanOrEqualTo(securityUser.getCreateDateEnd());
            }
            if (null != securityUser.getUpdateBy()){
                criteria.andUpdateByEqualTo(securityUser.getUpdateBy());
            }
            if (null != securityUser.getUpdateDateBegin()){
                criteria.andUpdateDateGreaterThanOrEqualTo(securityUser.getUpdateDateBegin());
            }
            if (null != securityUser.getUpdateDateEnd()){
                criteria.andUpdateDateLessThanOrEqualTo(securityUser.getUpdateDateEnd());
            }
            if (null != securityUser.getAccountNonExpired()){
                criteria.andAccountNonExpiredEqualTo(securityUser.getAccountNonExpired());
            }
            if (null != securityUser.getAccountNonLocked()){
                criteria.andAccountNonLockedEqualTo(securityUser.getAccountNonLocked());
            }
            if (null != securityUser.getCredentialsNonExpired()){
                criteria.andCredentialsNonExpiredEqualTo(securityUser.getCredentialsNonExpired());
            }
            if (null != securityUser.getEnabled()){
                criteria.andEnabledEqualTo(securityUser.getEnabled());
            }
        }
        return example;
    }
}
